import bcrypt from 'bcryptjs'
import { Context } from 'koa';
const { getUserInfo } = require('../service/user.service')
const { userFormateError, userDoesNotExist, userLoginError, invalidPassword, invalidCaptcha, userAlreadyExited, userRegisterError, userIdIsNotExist, captchaIsnotExist } = require('../constants/err.type');
const userValidator = async (ctx: Context, next: () => Promise<void>) => {
    const { username, password, captcha } = ctx.request.body;
    // 合法性
    if (!username || !password) {
        ctx.app.emit('error', userFormateError, ctx)
        return;
    }
    if (!ctx.session.captcha) {
        return ctx.app.emit('error', captchaIsnotExist, ctx)
    }
    if (ctx.session.captcha !== captcha.toLowerCase()) {
        ctx.app.emit('error', invalidCaptcha, ctx)
        return;
    }
    await next();
}

const addUserValidator = async (ctx: Context, next: () => Promise<void>) => {
    const { logname, password } = ctx.request.body;
    // 合法性
    if (!logname || !password) {
        ctx.app.emit('error', userFormateError, ctx)
        return;
    }
    await next();
}

const verifyUser = async (ctx: Context, next: () => Promise<void>) => {
    const { logname } = ctx.request.body;
    // 合理性
    try {
        const res = await getUserInfo({ username: logname })
        if (res) {
            console.error('用户名已经存在', logname)
            ctx.app.emit('error', userAlreadyExited, ctx)
            return;
        }
    } catch (error) {
        console.error('用户信息错误')
        ctx.app.emit('error', userRegisterError, ctx)
        return;
    }
    await next();
}

// const cryptPassword = async (ctx: Context, next: () => Promise<void>) => {
//     const { password } = ctx.request.body;
//     const salt = bcrypt.genSaltSync(10);
//     // hash保存的是密文
//     const hash = bcrypt.hashSync(password, salt)
//     ctx.request.body.password = hash;
//     await next();
// }

const verifyLogin = async (ctx: Context, next: () => Promise<void>) => {
    // 1.判断用户是否存在
    const { username, password } = ctx.request.body;
    try {
        const res = await getUserInfo({ username })
        if (!res) {
            console.error('用户名不存在', username)
            ctx.app.emit('error', userDoesNotExist, ctx)
            return;
        }
        // 2.判断密码是否匹配
        if (password != res.password) {
            ctx.app.emit('error', invalidPassword, ctx)
            return;
        }
    } catch (error) {
        console.error(error)
        ctx.app.emit('error', userLoginError, ctx)
        return;
    }
    await next();
}

const userIdisExist = async (ctx: Context, next: () => Promise<void>) => {
    try {
        const id = ctx.params.id
        const res = await getUserInfo({ id })
        if(!res) {
            ctx.app.emit('error', userIdIsNotExist, ctx)
            return;
        }
    } catch (error) {
        console.error('根据id获取普通用户失败', error)
    }
    await next();
}

module.exports = {
    userValidator,
    verifyUser,
    // cryptPassword,
    addUserValidator,
    verifyLogin,
    userIdisExist
}